# 预设菜单
import bpy
from bpy.types import Operator, Menu
from bl_operators.presets import AddPresetBase


class OBJECT_MT_display_presets(Menu):
    bl_label = "Object Display Presets"
    preset_subdir = "object/display"
    preset_operator = "script.execute_preset"
    draw = Menu.draw_preset


class AddPresetObjectDisplay(AddPresetBase, Operator):
    '''Add a Object Display Preset'''
    bl_idname = "camera.object_display_preset_add"
    bl_label = "Add Object Display Preset"
    preset_menu = "OBJECT_MT_display_presets"

    # 用于存储预设值的变量 <exec>  variable used for all preset values
    preset_defines = ["obj = bpy.context.object"]

    # 存储在预设中的属性列表 <eval> properties to store in the preset
    preset_values = ["obj.display_type",
                     "obj.show_bounds",
                     "obj.display_bounds_type",
                     "obj.show_name",
                     "obj.show_axis",
                     "obj.show_wire"]

    # 存储预设的位置
    # Mac: /用户/用户名/资源库/Application Support/Blender/2.92/scripts/presets/object/display
    # Win: C:/Users/用户名/....../2.92/scripts/presets/object/display
    preset_subdir = "object/display"


# 显示在已存在的面板中  Display into an existing panel
def panel_func(self, context):
    layout = self.layout

    row = layout.row(align=True)
    row.menu(OBJECT_MT_display_presets.__name__, text=OBJECT_MT_display_presets.bl_label)
    row.operator(AddPresetObjectDisplay.bl_idname, text="", icon='ZOOM_IN')
    row.operator(AddPresetObjectDisplay.bl_idname, text="", icon='ZOOM_OUT').remove_active = True


classes = (OBJECT_MT_display_presets,
           AddPresetObjectDisplay)


def register():
    for cls in classes:
        bpy.utils.register_class(cls)
    # 在 物体显示栏 中 添加绘制菜单
    bpy.types.OBJECT_PT_display.prepend(panel_func)
    bpy.types.OBJECT_PT_display.append(panel_func)


def unregister():
    for cls in classes:
        bpy.utils.unregister_class(cls)
    bpy.types.OBJECT_PT_display.remove(panel_func)


if __name__ == "__main__":
    register()
